package com.wman.lib.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    /**
     * yyyy年MM月dd日 HH:mm:ss
     */
    public static final String PATTERN_TEXT_SECOND = "yyyy年MM月dd日 HH:mm:ss";
    /**
     * yyyy年MM月dd日 HH:mm
     */
    public static final String PATTERN_TEXT_MINUTE = "yyyy年MM月dd日 HH:mm";
    /**
     * yyyy年MM月dd日
     */
    public static final String PATTERN_TEXT = "yyyy年MM月dd日";
    /**
     * yyyy年MM月dd日 a
     */
    public static final String PATTERN_NOON = "yyyy年MM月dd日 a";
    /**
     * yyyy-MM-dd
     */
    public static final String PATTERN_DAY = "yyyy-MM-dd";
    /**
     * yyMMdd
     */
    public static final String PATTERN_DAY_2 = "yyMMdd";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String PATTERN_TIME = "yyyy-MM-dd HH:mm:ss";
    /**
     * MM月dd日
     */
    public static final String PATTERN_MONTH_DAY = "MM月dd日";
    /**
     * MM月dd日 HH时mm分
     */
    public static final String PATTERN_MONTH_DAY_HOUR_MINUTE = "MM月dd日 HH时mm分";
    /**
     * HH:mm
     */
    public static final String PATTERN_HOUR_MINUTE = "HH:mm";

    /**
     * 
     * @Title: getTodayStart
     * @Description: 获取今天凌晨0点
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date getTodayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: getTodayEnd
     * @Description: 获取当前最后时间
     * @param: @param date
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date getTodayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 59);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: getYestoday
     * @Description: 获取昨天凌晨0点
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date getYestodayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: getCurrentWeekStart
     * @Description: 获取本周开始时间
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date getWeekStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        week = week == 1 ? 6 : week - 2;
        calendar.add(Calendar.DAY_OF_YEAR, -week);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: getCurrentWeekEnd
     * @Description: 获取本周结束时间
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date getWeekEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getWeekStart(date));
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.add(Calendar.SECOND, -1);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: addDay
     * @Description: 增减天数
     * @param: @param day
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, day);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: addMonth
     * @Description: 增减月数
     * @param: @param month
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: addDayToStart
     * @Description: 增减天数，并设置为凌晨0点
     * @param: @param date
     * @param: @param day
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date addDayToStart(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: addHour
     * @Description: 小时加减
     * @param: @param date
     * @param: @param hour
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date addHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: addMinute
     * @Description: 分钟加减
     * @param: @param date
     * @param: @param minute
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date addMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: addSecend
     * @Description: 秒加减
     * @param: @param date
     * @param: @param secend
     * @param: @return
     * @return: Date
     * @throws
     */
    public static Date addSecend(Date date, int secend) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, secend);
        return calendar.getTime();
    }

    /**
     * 
     * @Title: getSubDay
     * @Description: 计算量日期的天数差值
     * @param: @param startDate
     * @param: @param endDate
     * @param: @return
     * @return: int
     * @throws
     */
    public static int getSubDay(Date startDate, Date endDate) {
        long time = endDate.getTime() - startDate.getTime();
        return (int) (time / 1000 / 60 / 60 / 24);
    }

    /**
     * @Description: 计算日期的自然天数差
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getSubNatureDay(Date startDate, Date endDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int year1 = calendar.get(Calendar.YEAR);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(endDate);
        int year2 = calendar.get(Calendar.YEAR);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);
        int sub = year2 - year1;
        if (sub == 0) {
            return day2 - day1;
        } else if (sub == 1) {
            // 闰年判断
            if (year1 % 400 == 0) {
                return 366 - day1 + day2;
            } else {
                return 365 - day1 + day2;
            }
        } else {
            int day = 0;
            for (int i = year1 + 1; i >= year2; i++) {
                // 闰年判断
                if (i % 400 == 0) {
                    day += 366;
                } else {
                    day += (i % 4 == 0 && i % 100 > 0) ? 366 : 365;
                }
            }
            return day2 + day1 + day;
        }
    }

    /**
     * 查询本季度开始时间
     * @param inDate 输入的日期时间
     * @return 本季度第一天的日期时间
     * @throws ParseException
     */
    public static Date getCurrentQuarterStartTime(Date inDate) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inDate);
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month >= 1 && month <= 3) {
            calendar.set(Calendar.MONTH, 0);
        } else if (month >= 4 && month <= 6) {
            calendar.set(Calendar.MONTH, 3);
        } else if (month >= 7 && month <= 9) {
            calendar.set(Calendar.MONTH, 6);
        } else if (month >= 10 && month <= 12) {
            calendar.set(Calendar.MONTH, 9);
        }
        calendar.set(Calendar.DATE, 1);
        DateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date returnTime = longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
        return returnTime;
    }

    /**
     * 查询本季度结束时间
     * @param inDate 输入的日期时间
     * @return 本季度最后一天的日期时间
     * @throws ParseException
     */
    public static Date getCurrentQuarterEndTime(Date inDate) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inDate);
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month >= 1 && month <= 3) {
            calendar.set(Calendar.MONTH, 2);
            calendar.set(Calendar.DATE, 31);
        } else if (month >= 4 && month <= 6) {
            calendar.set(Calendar.MONTH, 5);
            calendar.set(Calendar.DATE, 30);
        } else if (month >= 7 && month <= 9) {
            calendar.set(Calendar.MONTH, 8);
            calendar.set(Calendar.DATE, 30);
        } else if (month >= 10 && month <= 12) {
            calendar.set(Calendar.MONTH, 11);
            calendar.set(Calendar.DATE, 31);
        }
        DateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date returnTime = longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
        return returnTime;
    }

    /**
     * 获取本月开始时间
     * @param  
     * @return 
     */
    public static Date getMonthStart(Date inDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inDate);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获取本月结束时间
     * @param  
     * @return 
     */
    public static Date getMonthEnd(Date inDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inDate);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 日期转星期
     * 
     * @param date
     * @return
     */
    public static String dateToWeek(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String dateFormat(Date date, String formatPatten) {
        DateFormat df = new SimpleDateFormat(formatPatten);
        if (date == null) {
            return null;
        }
        return df.format(date);
    }

    /**
     * @Description: 计算天、小时差值，小于一小时直接返回一小时之内
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getSubTime(Date startDate, Date endDate) {
        long time = endDate.getTime() - startDate.getTime();
        String result;
        if (time / 1000 / 60 < 60) {
            result = "一小时之内";
        } else if (time / 1000 / 60 / 60 < 24) {
            result = time / 1000 / 60 / 60 + "小时后";
        } else {
            result = time / 1000 / 60 / 60 / 24 + "天后";
        }
        return result;
    }

    /**
     * @Description: 按天倒计时
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getSubDayFull(Date startDate, Date endDate) {
        // 当前时区起点时间为1970年1月1日8点,计算时需减去当前时区偏移量
        Calendar calendar = Calendar.getInstance();
        int offset = calendar.getTimeZone().getRawOffset();
        calendar.setTimeInMillis(endDate.getTime() - startDate.getTime() - offset);
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String result = null;
        if (day > 0) {
            result = day + "天";
        }
        if (hour > 0) {
            result += hour + "小时";
        }
        if (minute > 0) {
            result += minute + "分";
        }
        if (second > 0) {
            result += second + "秒";
        }
        return result;
    }
    
    /**
     * @Description: 按天计数
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getSubDayTime(Date startDate, Date endDate) {
    	if (startDate == null || endDate == null) {
			return null;
		}
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTimeInMillis(endDate.getTime() - startDate.getTime());
    	int day = calendar.get(Calendar.DAY_OF_YEAR);
    	int hour = calendar.get(Calendar.HOUR_OF_DAY);
    	int minute = calendar.get(Calendar.MINUTE);
    	String result = null;
    	if (day > 0) {
    		result = day + "天";
    	}
    	if (hour > 0) {
    		result += hour + "小时";
    	}
    	if (minute > 0) {
    		result += minute + "分";
    	}
    	return result;
    }

    /**
     * @Description: 本年内返回月日，往年的则返回年月日
     * @param date
     * @return
     */
    public static String hanldeDate(Date date) {
        String result = null;
        if (date != null) {
            Calendar now = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int subDay = now.get(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR);
            if (subDay == 0) {
                result = "今天";
            } else if (subDay == 1) {
                result = "昨天";
            } else {
                int subYear = now.get(Calendar.YEAR) - now.get(Calendar.YEAR);
                if (subYear == 0) {
                    result = dateFormat(date, PATTERN_MONTH_DAY);
                } else {
                    result = dateFormat(date, PATTERN_TEXT);
                }
            }
        }
        return result;
    }
}
